/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package personal.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.InputSource;

/**
 *
 * @author PHATNBSE61057
 */
public class SchemaValidation {

    public SchemaValidation() {
    }

    public void validationBookINFO() {
        try {
            //CHECK VALIDATION USING SCHEMA - SAX PARSER
            JAXBContext jc = JAXBContext.newInstance("sample.jaxb");
            Unmarshaller u = jc.createUnmarshaller();
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File("./web/xml/bookValidation.xsd"));
            Validator validator = schema.newValidator();
            InputSource inputFile = new InputSource(new BufferedReader(new FileReader("./web/xml/addingBook.xml")));
            validator.validate(new SAXSource(inputFile));
            System.out.println("======");
            File f = new File("./web/xml/addingBook.xml");
            personal.validation.Book items = (personal.validation.Book) u.unmarshal(f);
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
